package com.example.sup.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RedisConfig
 * @Description {TODO}
 * @Author 智邦教育.陈超
 * @Date 10:33 上午 2024/7/12
 * @Version 1.0
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置key的序列化方式：string
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置value的序列化方式:json
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        //hash配置field和value的序列化方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }
    /**
     * spring cache 模块必须配置RedisCacheManager 指定缓存的序列化方式、过期时间(全局的过期时间)、禁用缓存空值等等配置
     * @param redisConnectionFactory
     * @return
     */
    @Primary
    @Bean
    RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory){
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
                .defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(RedisSerializer.json()))//设置key的序列化方式
                .serializeKeysWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(RedisSerializer.string()))
                .disableCachingNullValues()//禁用缓存空值
                .entryTtl(java.time.Duration.ofSeconds(60*60*24));//设置统一的过期时间：1天

        return RedisCacheManager
                .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheConfiguration).build();

    }

    @Bean
    public ValueOperations valueOperations(RedisTemplate<String,String > redisTemplate){
        return redisTemplate.opsForValue();
    }
    @Bean
    public ZSetOperations zSetOperations(RedisTemplate<String,Object > redisTemplate){
        return redisTemplate.opsForZSet();
    }
    @Bean
    public HashOperations hashOperations(RedisTemplate<String,Object > redisTemplate){
        return redisTemplate.opsForHash();
    }
    @Bean
    public ListOperations listOperations(RedisTemplate<String,Object > redisTemplate){
        return redisTemplate.opsForList();
    }
    @Bean
    public SetOperations setOperations(RedisTemplate<String,Object > redisTemplate){
        return redisTemplate.opsForSet();
    }
}
